package net.wofly.sale.commodity.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.web.BaseController;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.commodity.domain.CommodityType;
import net.wofly.sale.commodity.service.ICommodityTypeService;
import net.wofly.sale.commodity.validator.CommodityTypeValidator;


@RestController
@RequestMapping("/api/commodity/commoditytype")
@Api(description = "商品类型接口")
public class CommodityTypeController extends BaseController{
	
	@Autowired
	ICommodityTypeService iCommodityTypeService ;
	
	@ApiOperation(value = "添加商品类型", notes = "添加商品类型")
	@ApiImplicitParam(name = "commodityType", value = "商品类型实体", required = true, dataType = "CommodityType", paramType = "body")
	@RequestMapping(value = "/addCommodityType", method = {RequestMethod.POST})
	@ResponseBody
	public ReturnBean<CommodityType> saveCommodityType(@RequestBody CommodityType commodityType) {
		
		String errorMessage = CommodityTypeValidator.validateAddCommodityTypeInParams(commodityType) ;
		
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(91011, errorMessage);
        }
        
        return iCommodityTypeService.saveCommodityType(commodityType) ;
	}
	
	@ApiOperation(value = "修改商品类型", notes = "修改商品类型")
	@ApiImplicitParam(name = "commodityType", value = "商品类型实体", required = true, dataType = "CommodityType", paramType = "body")
	@RequestMapping(value = "/updateCommodityType", method = {RequestMethod.POST})
	@ResponseBody
	public ReturnBean<CommodityType> updateCommodityType(@RequestBody CommodityType commodityType) {
		
		String errorMessage = CommodityTypeValidator.validateUpdateCommodityTypeInParams(commodityType) ;
		
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(91011, errorMessage);
        }
        
        return iCommodityTypeService.updateCommodityType(commodityType) ;
	}
	
	@ApiOperation(value = "删除商品类型", notes = "删除商品类型")
	@ApiImplicitParam(name = "commodityTypeID", value = "商品类型ID", required = true, dataType = "String", paramType = "form")
	@RequestMapping(value = "/delCommodityType", method = {RequestMethod.POST})
	@ResponseBody
	public ReturnBean<String> delCommodityType(String commodityTypeID) {
		
		String errorMessage = CommodityTypeValidator.validateCommodityTypeID(commodityTypeID);
		
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(91012, errorMessage);
        }
		
		return iCommodityTypeService.delete(commodityTypeID) ;
	}
	
	@ApiOperation(value = "分页获取所有商品类型", notes = "分页获取所有商品类型")
	@ApiImplicitParams({@ApiImplicitParam(name = "commodityTypeName", value = "商品类型名称", required = false, dataType = "String", paramType = "form"),
						@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "form"),
						@ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataType = "int", paramType = "form")
					})
	@RequestMapping(value = "/getAllCommodityType", method = {RequestMethod.POST})
	@ResponseBody
	public ReturnBean<PageWrapper<CommodityType>> getAllCommodityType(String commodityTypeName ,
															@RequestParam(defaultValue = "1", name = "pageNum")Integer pageNum, 
															@RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize) {
		
		return iCommodityTypeService.findJiuMaoCommodityTypeByPage(commodityTypeName ,pageNum ,pageSize) ;
	}


}
